package landyer.tools.jdbcGenerator;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.FileCopyUtils;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        String packageName = "gen";
        String root = "C:\\Users\\PC0801\\IdeaProjects\\reports\\src\\main\\java\\";
        String configLocation = "applicationContext-enterprise.xml";
        List<String> notContains = Arrays.asList("sys_org");

//        doWork(packageName, root, configLocation, notContains, null, TableWapper.MYSQL,false);
    }

    public static void doWork(String packageName, String root, String configLocation, List<String> notContains, String schema, int type,boolean onlySave) throws SQLException, IOException, ClassNotFoundException {
        doWork(packageName, root, configLocation, notContains,null, schema, type,onlySave);
    }

    public static void doWork(String packageName, String root, String configLocation, List<String> notContains, List<String> onlys, String schema, int type,boolean onlySave) throws SQLException, IOException, ClassNotFoundException {
        ApplicationContext app = new ClassPathXmlApplicationContext(configLocation);
        DataSource ds = app.getBean(DataSource.class);
        Connection connection = ds.getConnection();

        //String root = "/Volumes/mbak/magus/codes/eia-parent/eia-background/src/main/java/";
        String dir = root + packageName.replaceAll("\\.", "/");
        System.out.println(dir);
        String pojoDir = dir + "/domain/";
        String daoDir = dir + "/dao/";
        mkdirs(pojoDir);
        mkdirs(daoDir);

        TableWapper tw = null;
        if (type == TableWapper.ORACLE) {
            schema = schema.toUpperCase();
            tw = new OracleTableMapper();
        } else if (type == TableWapper.MYSQL) {
            tw = new MysqlTableMapper();
        }
        List<String> tables = null;
        if (onlys != null && onlys.size() > 0) {
            tables = onlys;
        } else {
            tables = tw.getTables(connection, schema);
        }
        for (String table : tables) {

            try {
                if (notContains==null || (notContains!=null && !notContains.contains(table))) {
                    System.out.println(table);
                    System.out.println("**************");
                    String tableName = table;
                    ModleGenerator generator = new ModleGenerator(tw.getColumns(connection, tableName), tableName,onlySave);

                    String daoStr = generator.genertorDao(packageName);
                    //System.out.println(daoStr);
                    FileCopyUtils.copy(new StringReader(daoStr), new FileWriter(daoDir + generator.getclassName() + "Dao.java"));
                    System.out.println("************");
                    String pojoStr = generator.genertorPojo(packageName);
                    FileCopyUtils.copy(new StringReader(pojoStr), new FileWriter(pojoDir + generator.getclassName() + ".java"));
                    //System.out.println(pojoStr);
                }
            } catch (Exception e) {
                System.out.println("处理" + table + "出错!" + e.getMessage());
            }
        }
    }

    private static void mkdirs(String pojoDir) {
        File f = new File(pojoDir);
        if (!f.exists()) {
            f.mkdirs();
        }
    }
}
